﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MinistryStandardPlatformEntity;
using MinistryStandardPlatformWeb.Models;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;

namespace MinistryStandardPlatformWeb.Controllers
{

    public class ReportCenterController : Controller
    {
        //
        // GET: /ReportCenter/
        private MSPDataContext context = new MSPDataContext();

        [MenuAuthorized("4",Order=1)]
        [IsLogin(Order=0)]
        public ActionResult Index()
        {
            return View();
        }

        //来自客户端的请求，免登陆，不显示layout
        public ActionResult IndexWithoutLayoutAutoLogin(string userId,string password)
        {
            HashAlgorithm ha = SHA1CryptoServiceProvider.Create();
            byte[] bytes=ha.ComputeHash(Encoding.UTF8.GetBytes(password));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                sBuilder.Append(bytes[i].ToString("x2"));
            }

            password = sBuilder.ToString().ToUpper();
            List<LoginUsers> users = context.LoginUsers.Where(u => u.UserId == userId && u.LoginPassword == password).ToList();
            if (users.Count() == 1)
            {
                if (users[0].userState == "启用")
                {
                    UserＭodel user = new UserＭodel();
                    user.UserId = users[0].UserId;
                    user.UnitId = users[0].CustomerID;
                    user.UnitName = users[0].CustomerName;

                    List<string> rights = context.MenuRights.Where(r => r.UserId == userId && r.RightType == null).Select(u => u.MenuId).ToList();
                    user.MenuRights = rights;

                    //设置用cookie方式验证
                    FormsAuthentication.SetAuthCookie(userId, false);

                    //将认证信息写入到cookie中
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        userId,
                        DateTime.Now,
                        DateTime.Now.Add(FormsAuthentication.Timeout),
                        false,
                        user.ToString());
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                    cookie.Path = FormsAuthentication.FormsCookiePath;

                    Response.Cookies.Add(cookie);

                    return View();
                }
                else
                {
                    @ViewBag.Message = "该账户已停用";
                    //Response.Write("<script>alert('该账户已停用')</script>");
                }
            }
            else
            {
                @ViewBag.Message = "用户名或密码错误";
                //Response.Write("<script>alert('用户名或密码错误')</script>");
            }
            return View("ActionResult");
        }

        [MenuAuthorized("4", Order = 1)]
        [IsLogin(Order = 0)]
        public ActionResult Tree()
        {
            List<ZTreeNode> nodes = new List<ZTreeNode>();
            nodes = (from r in context.RightsDetails
                    where r.HasRight == true &&
                    r.UserId==CommonFunctions.GetLoginUser().UserId
                    join v in context.InfoVehicle
                    on r.NodeId equals v.Vehiclenum
                    into rv
                    from rvd in rv.DefaultIfEmpty()
                    select new ZTreeNode()
                    {
                        id = r.NodeId,
                        pId=r.ParentNodeId,
                        name=r.NodeName,
                        sim=rvd==null?"":rvd.SIM,
                        icon = (r.NodeTYPE == true) ? "/Content/themes/zTreeStyle/img/diy/1_open.png" : "/Content/themes/zTreeStyle/img/diy/car.png",
                        open=(r.ParentNodeId==null)?true:false
                    }).ToList();
            ViewBag.nodesJson = CommonFunctions.ListToJsonStr(nodes);
            return PartialView();
        }


        public void ClearSession()
        {
            if (Session["SearchData"] != null)
            {
                Session["SearchData"] = null;
            }
        }
        public ActionResult DateSearch()
        {
            ViewBag.Date = DateTime.Now.ToString(CommonDictionary.DateStrFormat);
            return PartialView();
        }

        public ActionResult SimpleSearch()
        {
            return PartialView();
        }

        public ActionResult DateTimeSearch()
        {
            ViewBag.StartTime = "2017-8-13 00:00:00";// DateTime.Now.ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.EndTime = "2017-8-16 00:00:00"; //DateTime.Now.AddDays(1).ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            return PartialView();
        }

        public ActionResult OneCarDateTimeSearch()
        {
            ViewBag.StartTime = "2017-8-13 00:00:00";// DateTime.Now.ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.EndTime = "2017-8-16 00:00:00"; //DateTime.Now.AddDays(1).ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            return PartialView();
        }

        /*未熄火停车*/
        public ActionResult StopWithAccOnDateTimeSearch()
        {
            ViewBag.StartTime = "2017-8-13 00:00:00";// DateTime.Now.ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.EndTime = "2017-8-16 00:00:00"; //DateTime.Now.AddDays(1).ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.StopInterval = "2";
            return PartialView();
        }

        /*进出区域明细*/
        public ActionResult InOutRegionDateTimeSearch()
        {
            ViewBag.StartTime = "2017-8-13 00:00:00";// DateTime.Now.ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.EndTime = "2017-8-16 00:00:00"; //DateTime.Now.AddDays(1).ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            return PartialView();
        }
        public ActionResult WarningDetailSearch()
        {
            ViewBag.StartTime = "2017-8-13 00:00:00";// DateTime.Now.ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.EndTime = "2017-8-16 00:00:00"; //DateTime.Now.AddDays(1).ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            return PartialView();
        }


        public ActionResult WarningTypes()
        {
            List<ZTreeNode> nodes=new List<ZTreeNode>();
            nodes.Add(new ZTreeNode(){
                id="-1",
                name="所有报警类型",
                pId=null,
                open=true,
                @checked=true
            });
            foreach (KeyValuePair<CommonDictionary.EWarnType, string> pair in CommonDictionary.WarningType)
            {
                nodes.Add(new ZTreeNode()
                {
                    id = ((int)pair.Key).ToString(),
                    name = pair.Value,
                    pId = "-1",
                    @checked=true
                });
            }
            ViewBag.Types = CommonFunctions.ListToJsonStr(nodes);
            return PartialView();
        }

        public ActionResult OilAnalysisSearch()
        {
            ViewBag.StartTime = "2017-8-13 00:00:00";// DateTime.Now.ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            ViewBag.EndTime = "2017-8-16 00:00:00"; //DateTime.Now.AddDays(1).ToString(CommonDictionary.DateStrFormat) + " 00:00:00";
            return PartialView();
        }
    }
}
